Repository navigation
chore(): improve new link checker - #7547
mhartington wants to merge 8 commits into
Conversation
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
WalkthroughReworks the external-links checker into a two‑phase, concurrency‑configurable flow with per‑file aggregation and JSON reporting; adds a GitHub Actions workflow to run and format the checker on PRs; removes the old Lychee workflow; and updates an npm script argument terminator. Changes
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes 🚥 Pre-merge checks | ✅ 1 | ❌ 2❌ Failed checks (1 warning, 1 inconclusive)
✅ Passed checks (1 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
🍈 Lychee Link Check Report3664 links: ✅ All links are working!Full Statistics Table
|
There was a problem hiding this comment.
🧹 Nitpick comments (2)
apps/docs/scripts/lint-external-links.ts (2)
297-316: Two-phase design (collect then validate) is a solid architectural choice.Separating link extraction from URL validation is cleaner and more efficient — you deduplicate URLs before hitting the network, and the two phases have independent concurrency knobs. Good stuff.
One note on lines 314–315:
perFileOccurrences.push(...)andextractedLinks += ...mutate shared state from within concurrent workers. This is safe in Node.js because there's noawaitbetween the mutation and the next yield point (JS is cooperative, not preemptive). Just calling it out so future readers don't panic — a brief inline comment would help.📝 Optional: add a clarifying comment
+ // Safe to mutate – no `await` between here and the next iteration, + // so no other worker can interleave. perFileOccurrences.push(localOccurrences); extractedLinks += links.length;🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@apps/docs/scripts/lint-external-links.ts` around lines 297 - 316, The concurrent worker body mutates shared variables perFileOccurrences and extractedLinks which can look unsafe; add a brief inline comment right above the perFileOccurrences.push(localOccurrences) and extractedLinks += links.length lines explaining that this mutation is safe because runWithConcurrency runs on Node's single-threaded event loop (no preemptive multithreading) and there is no await between the read/modify/write steps, so the operations are atomic in this context; reference the worker function passed to runWithConcurrency and the local variable localOccurrences in the comment so readers can locate the code quickly.
267-279:mergeOccurrencesaliases source arrays into target — consider cloning.On line 277, when a URL isn't yet in the target map, you set the reference directly:
target.set(url, occurrences). This meanstargetandsourcenow share the same array instance. If anything ever mutates the source map's arrays after merging, the target map silently reflects those changes too.In your current usage this is safe because
perFileOccurrencesentries aren't touched after merge. But it's a subtle footgun for future maintainers. A shallow copy ([...occurrences]) on line 277 would eliminate this coupling at negligible cost.♻️ Optional: break the shared reference
- target.set(url, occurrences); + target.set(url, [...occurrences]);🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@apps/docs/scripts/lint-external-links.ts` around lines 267 - 279, The mergeOccurrences function currently aliases the source array into the target by calling target.set(url, occurrences), which creates a shared mutable reference; change the behavior so you insert a shallow copy of the occurrences array when the URL is new (i.e., set the target entry to a new array containing the same items instead of the original array) to avoid accidental future mutations of source affecting target while preserving current semantics; update the logic in mergeOccurrences where target.set is called for a non-existing key to store a copied array rather than the original.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@apps/docs/scripts/lint-external-links.ts`:
- Around line 297-316: The concurrent worker body mutates shared variables
perFileOccurrences and extractedLinks which can look unsafe; add a brief inline
comment right above the perFileOccurrences.push(localOccurrences) and
extractedLinks += links.length lines explaining that this mutation is safe
because runWithConcurrency runs on Node's single-threaded event loop (no
preemptive multithreading) and there is no await between the read/modify/write
steps, so the operations are atomic in this context; reference the worker
function passed to runWithConcurrency and the local variable localOccurrences in
the comment so readers can locate the code quickly.
- Around line 267-279: The mergeOccurrences function currently aliases the
source array into the target by calling target.set(url, occurrences), which
creates a shared mutable reference; change the behavior so you insert a shallow
copy of the occurrences array when the URL is new (i.e., set the target entry to
a new array containing the same items instead of the original array) to avoid
accidental future mutations of source affecting target while preserving current
semantics; update the logic in mergeOccurrences where target.set is called for a
non-existing key to store a copied array rather than the original.
External Link Check Report
Failures
|
There was a problem hiding this comment.
🧹 Nitpick comments (2)
apps/docs/scripts/lint-external-links.ts (1)
153-153:MARKDOWN_IMAGE_REGEX.lastIndex = 0is dead code — the regex is never iterated.
MARKDOWN_IMAGE_REGEXis reset here but has no correspondingwhileloop incollectFileLinks. Image links are already excluded by the(?<!!)negative lookbehind inMARKDOWN_LINK_REGEX(line 30), so this reset serves no purpose.🧹 Proposed cleanup
- MARKDOWN_IMAGE_REGEX.lastIndex = 0; MARKDOWN_LINK_REGEX.lastIndex = 0; AUTOLINK_REGEX.lastIndex = 0; HTML_ANCHOR_REGEX.lastIndex = 0;🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@apps/docs/scripts/lint-external-links.ts` at line 153, Remove the dead reset of the MARKDOWN_IMAGE_REGEX in collectFileLinks: the statement "MARKDOWN_IMAGE_REGEX.lastIndex = 0" is unnecessary because MARKDOWN_IMAGE_REGEX is never iterated and image links are already excluded by the negative lookbehind in MARKDOWN_LINK_REGEX; delete that line (referencing MARKDOWN_IMAGE_REGEX and the collectFileLinks function) to clean up the codebase..github/workflows/external-links.yml (1)
33-42: Add a step-level timeout to prevent runaway link-checker runs.Without a
timeout-minutescap, a large docs site with hundreds of slow-responding external URLs could stall the job indefinitely (the script-levelTIMEOUT_MSonly gates individual URL fetches, not the whole run).⏱️ Proposed fix
- name: Run external link checker id: external-links + timeout-minutes: 30 shell: bash🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In @.github/workflows/external-links.yml around lines 33 - 42, The "Run external link checker" step (id: external-links) needs a step-level timeout to avoid runaway runs; add a timeout-minutes value (for example timeout-minutes: 30 or an appropriate cap for your CI budget) to the step definition that runs "pnpm run lint:external-links" so the whole step is terminated if it exceeds that limit, keeping the existing commands and exit handling intact.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In @.github/workflows/external-links.yml:
- Around line 33-42: The "Run external link checker" step (id: external-links)
needs a step-level timeout to avoid runaway runs; add a timeout-minutes value
(for example timeout-minutes: 30 or an appropriate cap for your CI budget) to
the step definition that runs "pnpm run lint:external-links" so the whole step
is terminated if it exceeds that limit, keeping the existing commands and exit
handling intact.
In `@apps/docs/scripts/lint-external-links.ts`:
- Line 153: Remove the dead reset of the MARKDOWN_IMAGE_REGEX in
collectFileLinks: the statement "MARKDOWN_IMAGE_REGEX.lastIndex = 0" is
unnecessary because MARKDOWN_IMAGE_REGEX is never iterated and image links are
already excluded by the negative lookbehind in MARKDOWN_LINK_REGEX; delete that
line (referencing MARKDOWN_IMAGE_REGEX and the collectFileLinks function) to
clean up the codebase.
ℹ️ Review info
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (4)
.github/workflows/external-links.yml.github/workflows/lychee.ymlapps/docs/scripts/lint-external-links.tspackage.json
💤 Files with no reviewable changes (1)
- .github/workflows/lychee.yml
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (4)
.github/workflows/external-links.yml (4)
27-27: Upgrade to Node 22 (or 24) — Node 20 reaches EOL on April 30, 2026.Node.js 20.x reaches end-of-life on April 30, 2026, which is only ~2 months away. Node.js 20 has been in Maintenance LTS since October 22, 2024, receiving only critical bug fixes and security patches. Setting up a brand-new workflow on a near-EOL runtime means you'll be forced to update it very soon.
Node 22 is currently in Maintenance LTS (EOL April 30, 2027), while Node 24 is the Active LTS release (EOL April 30, 2028). Node 22 is the safest drop-in upgrade.
⬆️ Proposed fix — use Node 22
- node-version: "20" + node-version: "22"🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In @.github/workflows/external-links.yml at line 27, The workflow currently pins the runtime to node-version: "20" which will reach EOL soon; update the workflow to use a supported LTS (change node-version: "20" to "22" or "24") in the external-links.yml GitHub Actions job definition so the CI uses Node 22 (recommended) or Node 24 instead; ensure any job/matrix entries referencing node-version (e.g., the node-version: "20" key) are updated consistently and verify any Node-specific setup steps still work with the new version.
19-19: Consider pinning actions to full commit SHAs for supply-chain safety.All four actions use mutable semver tags. A tag can be silently re-pointed to a different commit — especially relevant for the community-owned
marocchino/sticky-pull-request-comment. Pinning to a specific SHA makes the used version immutable and auditable.🔒 Example SHA-pinned alternatives (verify latest SHAs before using)
- uses: actions/checkout@v4 + uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4 - uses: pnpm/action-setup@v4 + uses: pnpm/action-setup@fe02b74ar8d0fad1bea82ccd5a16c7e72d81e4f3 # v4 - uses: actions/setup-node@v4 + uses: actions/setup-node@39370e3970a6d050c480ffad4ff0ed4d3fdee5af # v4 - uses: marocchino/sticky-pull-request-comment@v2 + uses: marocchino/sticky-pull-request-comment@773df9f8fc2f941882b5e9bb5c2f9b8a5c5a1c77 # v2
⚠️ The SHAs above are illustrative — fetch the real values viagh api repos/<owner>/<repo>/git/ref/tags/<tag>before committing.Also applies to: 22-22, 25-25, 129-129
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In @.github/workflows/external-links.yml at line 19, Replace mutable semver tags for third-party actions with immutable commit SHAs: find usages like actions/checkout@v4, actions/setup-node@*, marocchino/sticky-pull-request-comment@*, and the other action referenced in the workflow and replace their tag (e.g., `@v4`) with the full commit SHA for that tag (e.g., @<long-sha>); obtain the correct SHA using the GitHub API (gh api repos/<owner>/<repo>/git/ref/tags/<tag>) and update each action reference so the workflow pins to the exact commit SHA.
3-8: Consider adding apathsfilter to avoid running the checker on unrelated PRs.As written, the workflow runs on every PR regardless of which files changed. Since the checker scans docs content, PRs that touch only CI configs, scripts, or other non-docs files still pay the full cost of a network-bound link scan.
⚡ Example `paths` filter
on: pull_request: + paths: + - 'apps/docs/**' + - '.github/workflows/external-links.yml'Adjust the globs to match wherever the markdown/MDX source files actually live.
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In @.github/workflows/external-links.yml around lines 3 - 8, The workflow currently triggers on every pull request (see the pull_request trigger block and concurrency group), causing unnecessary runs; add a paths filter under the pull_request trigger (e.g., a paths: list with your repository's markdown/MDX globs such as docs/**, content/**, **/*.md, **/*.mdx or similar) so the job only runs when documentation files change, and adjust the globs to match where your markdown/MDX live; keep the existing concurrency group unchanged.
78-79: Guard the integer comparison against a non-numericfailedLinksvalue.
jq -r '.failedLinks // 0'handlesnull/absent, but if the TypeScript script ever emitsfailedLinksas a string (note the// "n/a"fallbacks used elsewhere in the samejqcalls), bash's-gtwill abort withinteger expression expected. A simpletonumber?in the jq expression is the cleanest fix.🛡️ Proposed hardening
- FAILED_COUNT="$(jq -r '.failedLinks // 0' "$JSON_FILE")" + FAILED_COUNT="$(jq -r '(.failedLinks | tonumber?) // 0' "$JSON_FILE")"🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In @.github/workflows/external-links.yml around lines 78 - 79, The integer comparison fails if FAILED_COUNT is non-numeric; update the jq invocation that sets FAILED_COUNT (currently using ".failedLinks // 0") to coerce to a number using jq's tonumber? before falling back so FAILED_COUNT is always numeric for the bash "-gt" check; specifically modify the expression that produces FAILED_COUNT (and keep using the same variable name) to use tonumber? with a // 0 fallback so non-numeric or missing failedLinks becomes 0.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In @.github/workflows/external-links.yml:
- Around line 56-62: When jq validation fails (the if block checking JSON_FILE
with jq), write a minimal fallback report into the same report output path
(e.g., REPORT_FILE / formatted.md) before calling exit 1 so the downstream
"Comment external links report" step always has a file; specifically, in the
failure branch that currently echoes "Invalid JSON in $JSON_FILE" and prints
OUTPUT_FILE, create or echo a simple error markdown summary into $REPORT_FILE
(or the same variable used later to generate formatted.md) containing the error
message and exit status, then exit 1; make sure to reference the existing shell
variables JSON_FILE, OUTPUT_FILE and REPORT_FILE/ formatted.md so the comment
step can find the fallback file.
---
Nitpick comments:
In @.github/workflows/external-links.yml:
- Line 27: The workflow currently pins the runtime to node-version: "20" which
will reach EOL soon; update the workflow to use a supported LTS (change
node-version: "20" to "22" or "24") in the external-links.yml GitHub Actions job
definition so the CI uses Node 22 (recommended) or Node 24 instead; ensure any
job/matrix entries referencing node-version (e.g., the node-version: "20" key)
are updated consistently and verify any Node-specific setup steps still work
with the new version.
- Line 19: Replace mutable semver tags for third-party actions with immutable
commit SHAs: find usages like actions/checkout@v4, actions/setup-node@*,
marocchino/sticky-pull-request-comment@*, and the other action referenced in the
workflow and replace their tag (e.g., `@v4`) with the full commit SHA for that tag
(e.g., @<long-sha>); obtain the correct SHA using the GitHub API (gh api
repos/<owner>/<repo>/git/ref/tags/<tag>) and update each action reference so the
workflow pins to the exact commit SHA.
- Around line 3-8: The workflow currently triggers on every pull request (see
the pull_request trigger block and concurrency group), causing unnecessary runs;
add a paths filter under the pull_request trigger (e.g., a paths: list with your
repository's markdown/MDX globs such as docs/**, content/**, **/*.md, **/*.mdx
or similar) so the job only runs when documentation files change, and adjust the
globs to match where your markdown/MDX live; keep the existing concurrency group
unchanged.
- Around line 78-79: The integer comparison fails if FAILED_COUNT is
non-numeric; update the jq invocation that sets FAILED_COUNT (currently using
".failedLinks // 0") to coerce to a number using jq's tonumber? before falling
back so FAILED_COUNT is always numeric for the bash "-gt" check; specifically
modify the expression that produces FAILED_COUNT (and keep using the same
variable name) to use tonumber? with a // 0 fallback so non-numeric or missing
failedLinks becomes 0.
There was a problem hiding this comment.
Actionable comments posted: 1
♻️ Duplicate comments (1)
.github/workflows/external-links.yml (1)
44-91: Previous feedback on fallback report beforeexit 1has been addressed.The invalid-JSON branch now writes detailed error information (including the raw report and stderr) into
$REPORT_FILEbefore exiting, so the downstream comment step will always have content to post. Well handled.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In @.github/workflows/external-links.yml around lines 44 - 91, The "Parse and print external links report" step correctly handles the invalid-JSON branch by writing detailed information (using REPORT_FILE, JSON_FILE, OUTPUT_FILE) into the report before calling exit 1, so no code changes are required — keep the current logic that writes the raw report and stderr into "$REPORT_FILE" and appends it to "$GITHUB_STEP_SUMMARY" prior to exiting.
🧹 Nitpick comments (1)
.github/workflows/external-links.yml (1)
160-164: This step silently won't run if the "Comment" step fails, masking the real failure reason.The "Fail if broken links found" step uses the default
if: success()condition. If the comment step (line 153) fails — say, due to a permissions issue — this step is skipped entirely. The job does still fail, but the error message will point at the comment action rather than at broken links. This is a minor UX issue in failure diagnostics.Adding
if: always()here (and keeping the exit-code check) would ensure this step always reports clearly:Proposed change
- name: Fail if broken links found - if: ${{ steps.external-links.outputs.exit_code != '0' }} + if: ${{ always() && steps.external-links.outputs.exit_code != '0' }} run: | echo "External link checker failed." exit 1🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In @.github/workflows/external-links.yml around lines 160 - 164, The "Fail if broken links found" GitHub Actions step named "Fail if broken links found" should not be gated by the default success() condition; change its condition to always() while preserving the existing exit-code check (i.e., use if: ${{ always() && steps.external-links.outputs.exit_code != '0' }} or equivalent) so the step always runs and reports a clear "External link checker failed." exit when broken links are detected even if prior steps (like the "Comment" step) failed; update the step's if expression accordingly and keep the existing run block that echoes the message and exits 1.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In @.github/workflows/external-links.yml:
- Around line 118-151: The report block currently truncates REPORT_FILE (using
>) and thus overwrites the earlier "## External Link Check Report" header; fix
by either (Option A) changing the redirection in the happy-path block to append
(use >> "$REPORT_FILE" instead of > "$REPORT_FILE") so the prewritten header
remains, or (Option B) make the block self-contained by writing the header into
the same block that writes to REPORT_FILE (and remove the earlier header write)
so REPORT_FILE is complete when created; locate symbols REPORT_FILE, OUTPUT_FILE
and FAILED_COUNT in the block to implement the chosen change.
---
Duplicate comments:
In @.github/workflows/external-links.yml:
- Around line 44-91: The "Parse and print external links report" step correctly
handles the invalid-JSON branch by writing detailed information (using
REPORT_FILE, JSON_FILE, OUTPUT_FILE) into the report before calling exit 1, so
no code changes are required — keep the current logic that writes the raw report
and stderr into "$REPORT_FILE" and appends it to "$GITHUB_STEP_SUMMARY" prior to
exiting.
---
Nitpick comments:
In @.github/workflows/external-links.yml:
- Around line 160-164: The "Fail if broken links found" GitHub Actions step
named "Fail if broken links found" should not be gated by the default success()
condition; change its condition to always() while preserving the existing
exit-code check (i.e., use if: ${{ always() &&
steps.external-links.outputs.exit_code != '0' }} or equivalent) so the step
always runs and reports a clear "External link checker failed." exit when broken
links are detected even if prior steps (like the "Comment" step) failed; update
the step's if expression accordingly and keep the existing run block that echoes
the message and exits 1.
Summary by CodeRabbit
New Features
Chores
Other